# 设置目标文件夹路径
$targetFolder = "F:\BaiduSyncdisk\DailyUseGreen\1-567"

# 要保留的文件夹名称
$foldersToKeep = @("DWI", "T1_A", "T1_D", "T1_PRE", "T1_V", "T2")

# 获取 A 文件夹下的所有一级子文件夹
$subFolders = Get-ChildItem -Path $targetFolder -Directory

foreach ($subFolder in $subFolders) {
    # 定义 MR 目录的路径
    $mrFolderPath = Join-Path -Path $subFolder.FullName -ChildPath "MR"

    # 检查 MR 目录是否存在
    if (Test-Path -Path $mrFolderPath) {
        # 获取 MR 目录下的所有三级子文件夹
        $thirdLevelFolders = Get-ChildItem -Path $mrFolderPath -Directory

        foreach ($thirdFolder in $thirdLevelFolders) {
            # 检查三级文件夹名称是否在保留列表中
            if ($foldersToKeep -notcontains $thirdFolder.Name) {
                # 删除不在保留列表中的三级文件夹
                Write-Host "已删除文件夹: $($thirdFolder.FullName)"
                Remove-Item -Path $thirdFolder.FullName -Recurse -Force
            }
        }
    } else {
        Write-Host "未找到 MR 目录: $mrFolderPath"
    }
}
